package com.tanhua.server.controller;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementService;
import com.tanhua.server.service.TanHuaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementController {
    @Autowired
    private MovementService movementService;
    @Autowired
    private CommentService commentService;
    /**
     * 发布动态
     *
     * @param movement
     * @param imageContent
     * @return
     * @throws IOException
     */
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile[] imageContent) throws IOException {
        movementService.publishMovement(movement, imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询我的动态
     *
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity findByUserId(Long userId,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementService.findByUserId(userId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 好友动态查询
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementService.findmovements(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 推荐好友查询
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementService.findRecommend(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 单条动态查询
     *
     * @param movementId
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity findByid(@PathVariable("id") String movementId) {
        MovementsVo movementsVo = movementService.findByid(movementId);
        return ResponseEntity.ok(movementsVo);
    }

    /**
     * 动态点赞
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.like(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消动态点赞
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dishLike(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.dislike(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 动态喜欢
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) {
        Integer loveCount = commentService.love(movementId);
        return ResponseEntity.ok(loveCount);
    }

    /**
     * 取消动态喜欢
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId) {
        Integer loveCount = commentService.unlove(movementId);
        return ResponseEntity.ok(loveCount);
    }

    /**
     * 查询访客
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors() {
        List<VisitorsVo> list = movementService.visitors();
        return ResponseEntity.ok(list);
    }
}
